package common.mail;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;


import common.Conf;
import common.Logger;

public class MailSender {
	
	public MailSender() {
	}
	
	/**
	 * @param fromAddr
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @return
	 */
	public static boolean send(String fromAddr, String toAddr,
			String strSubject, String strMessage) {
		return send(fromAddr, toAddr, strSubject, strMessage, "html");
	}

	/**
	 * 占쏙옙占쏙옙 占쏙옙占�
	 * @param fromAddr
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @param strType
	 * @return
	 */
	public static boolean send(String fromAddr, String toAddr,
			String strSubject, String strMessage, String strType) {
		return sendMail(Conf.getConf("mail_server"), Conf
				.getInt("mail_server_port"), fromAddr, toAddr, strSubject,
				strMessage, strType);
	}

	/**
	 * @param fromAddr
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @param strType
	 * @param file
	 * @return
	 */
	public static boolean send(String fromAddr, String toAddr,
			String strSubject, String strMessage, String strType,
			AttachFile[] file) {
		return sendMail(Conf.getConf("mail_server"), fromAddr, toAddr,
				strSubject, strMessage, strType, file);
	}

	/**
	 * @param strHostName
	 * @param port
	 * @param fromAddr
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @param strType
	 * @return
	 */
	public static boolean sendMail(String strServerAddr, int port,
			String fromAddr, String toAddr, String strSubject,
			String strMessage, String strType) {

		try {

			Email email = null;
			if(strType.equals("html")){
				email = new HtmlEmail();
			}else{
				email = new MultiPartEmail();
			}

			email.setHostName(strServerAddr);
			email.setCharset("UTF-8");
			email.setFrom(fromAddr);
			email.addTo(toAddr);
			email.setSubject(strSubject);
			if(strType.equals("html")){
				((HtmlEmail) email).setHtmlMsg(strMessage);
			}else{
				email.setMsg(strMessage);
			}

			email.send();
			Logger.LOG_NOHUP.info("HTML mail send success");

			return true;
			
		} catch (EmailException e) {
			Logger.printError(e);
			return false;
		}
	}

	/**
	 * 
	 * @param strServerAddr
	 * @param fromAddr
	 * @param toAddr
	 * @param strSubject
	 * @param strMessage
	 * @param strType
	 * @param oAttachFile
	 * @return
	 */
	public static boolean sendMail(String strServerAddr, String fromAddr,
			String toAddr, String strSubject, String strMessage,
			String strType, AttachFile[] oAttachFile) {

		try {
			MultiPartEmail email = null ;
			if(strType.equals("html")){
				email = new HtmlEmail();
			}else{
				email = new MultiPartEmail();
			}

			email.setHostName(strServerAddr);

			email.setFrom(fromAddr);
			email.addTo(toAddr);
			email.setSubject(strSubject);			
			if(strType.equals("html")){
				((HtmlEmail) email).setHtmlMsg(strMessage);
			}else{
				email.setMsg(strMessage);
			}

			if (oAttachFile != null) {
				EmailAttachment attachment = new EmailAttachment();

				for (int i = 0; i < oAttachFile.length; i++) {
					attachment.setPath(oAttachFile[i].getFilePath());
					attachment.setDisposition(EmailAttachment.ATTACHMENT);
					attachment.setDescription("");
					attachment.setName(oAttachFile[i].getFileName());

					email.attach(attachment);
				}
			}

			email.send();
			Logger.LOG_NOHUP.info("File mail send success");

			return true;
			
		} catch (EmailException e) {
			Logger.printError(e);
			return false;
		}

	}

}
